package cn.tedu.apps_system.service.impl;

import cn.tedu.apps_system.base.response.JsonResult;
import cn.tedu.apps_system.base.response.ResultCode;
import cn.tedu.apps_system.mapper.AUserMapper;
import cn.tedu.apps_system.pojo.dto.AUserLoginDto;
import cn.tedu.apps_system.pojo.dto.UserLoginDto;
import cn.tedu.apps_system.pojo.dto.UserRegDto;
import cn.tedu.apps_system.pojo.entity.User;
import cn.tedu.apps_system.pojo.vo.AUserVO;
import cn.tedu.apps_system.pojo.vo.UserVO;
import cn.tedu.apps_system.service.AUserService;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AUserServiceImpl implements AUserService {
    @Autowired
    private AUserMapper auserMapper;

/*    @Override
    public JsonResult registerAUser(UserRegDto userRegDto) {
        log.debug("userRegDto: " + userRegDto);
        if (auserMapper.existsByUsername(userRegDto.getUsername()) > 0) {
            return new JsonResult(ResultCode.USERNAME_EXIST);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDto, user);
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        int n = auserMapper.reg(user);
        if (n > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(ResultCode.FAILED);
    }
*/


    @Override
    public JsonResult loginAUser(AUserLoginDto auserLoginDto, HttpSession session) {
//        public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
//            log.debug("userLoginParam = " + userLoginParam);
//            UserVO userVO = userMapper.selectByAdmin(userLoginParam);
//            if (userVO == null){
//                return new JsonResult(ResultCode.FAILED);
//            }
//            session.setAttribute("user", userVO);
//            return JsonResult.ok(userVO);
//        }
        log.debug("auserLoginDto: " + auserLoginDto);
        AUserVO auserVO = auserMapper.Adminlogin(auserLoginDto);
        if (auserVO == null) {
            return new JsonResult(ResultCode.LOGIN_FAILED);
        }
        session.setAttribute("user", auserVO);
        return JsonResult.ok(auserVO);
    }

    @Override
    public JsonResult listAUsers(@ApiIgnore HttpSession session) {
        AUserVO auserVO = (AUserVO) session.getAttribute("user");

        List<AUserVO> AuserVOList = auserMapper.Adminlist();

        //ops.set("notice_index", (Serializable) noticeListVOS, 30, TimeUnit.SECONDS);
//        cacheUtil.setExpire("notice_index", (Serializable) noticeListVOS, 30);
        return JsonResult.ok(AuserVOList);
    }
    @Override
    public JsonResult deleteAUser(@Range(min = 1, max = 100, message = "编号必须在1-100之间")
                                 @PathVariable Long id, @ApiIgnore HttpSession session) {
        //手动抛出异常,用于演示全局异常处理器用法
        if (id < 0){
            throw new IllegalArgumentException("id值非法!");
        }

        log.debug("id = " + id);
        AUserVO auserVO = (AUserVO) session.getAttribute("user");
        //登录状态校验代码在拦截器中
        int i = auserMapper.deleteAUser(id);
        if (i > 0){
            return JsonResult.ok();
        } else {
            // 非法请求,该用户正在尝试删除其他人的车辆
            return new JsonResult(ResultCode.VALIDATE_FAILED);
        }
    }

    @Override
    public JsonResult logoutAUser(HttpSession session) {
        session.removeAttribute("Auser");
        return JsonResult.ok();
    }
}